package com.memory.servlet;

import com.memory.dao.UserDao;
import com.memory.dao.impl.UserDaoImpl;
import com.memory.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;


@WebServlet(urlPatterns = "/UpdateServlet")
public class UpdateServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");

        // 1.根据id查找学生数据
        UserDaoImpl userDao = new UserDaoImpl();
        List<User> userList=null;
        String sno = request.getParameter("Sno");
        String sname = request.getParameter("Sname");
        String ssex = request.getParameter("Ssex");
        String sage = request.getParameter("Sage");
        String sdept = request.getParameter("Sdept");
        User user=new User();
        user.setSno(Integer.parseInt(sno));
        user.setSname(sname);
        user.setSsex(ssex);
        user.setSage(Integer.parseInt(sage));
        user.setSdept(sdept);
        try {
            userDao.update(user);
            userList = userDao.selectAll();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        request.setAttribute("userList",userList);
        request.getRequestDispatcher("userList.jsp").forward(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}